package control.render;

import java.awt.image.BufferedImage;

import javax.swing.SwingUtilities;

import control.profiler.Profiler;

import data.hierarchy.World;
import data.render.IRenderMethod;

public class RenderTask implements Runnable{
  private BufferedImage part;
  private double rayU;
  private double rayV;
  private double step;
  private World w;
  private IRenderMethod renderMethod;
  private BucketCompletedAlert completedAlert;
  
  public RenderTask(BufferedImage part, double rayU, double rayV, double step, World w, IRenderMethod renderMethod, BucketCompletedAlert completedAlert){
    super();
    this.part = part;
    this.rayU = rayU;
    this.rayV = rayV;
    this.step = step;
    this.w = w;
    this.renderMethod = renderMethod;
    this.completedAlert = completedAlert;
  }
  
  @Override
  public void run(){
    Profiler.getInstance().record(null);
    for(int y = 0; y < part.getHeight(); y++){
      for(int x = 0; x < part.getWidth(); x++){
        Profiler.getInstance().record(null);
        part.setRGB(x, y, renderMethod.getPixel(w.getCamera().getRay(rayU + x * step, rayV + y * step)).getRGB());
        Profiler.getInstance().addStatistics("pixel");
      }
    }
    Profiler.getInstance().addStatistics("bucket");
    SwingUtilities.invokeLater(completedAlert);
  }
}
